package cn.edu.sjtu.se.dslab.component.dp.parse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.edu.sjtu.se.dslab.component.design.DesignPattern;
import cn.edu.sjtu.se.dslab.component.design.DesignPatternType;
import cn.edu.sjtu.se.dslab.service.IDesignPatternService;

public class DesignPatternInstanceBean {
	
	@Resource(name="designPatternService")
	IDesignPatternService designPatternService;

	//the name of the <name> label
	static final String NAME = "name";
	//the type of the <type>
	static final String TYPE = "type";
	
	//the name of the <instances> label
	static final String INSTANCES = "instances";
	//the name of the <instance> label
	static final String INSTANCE = "instance";
	//the max id of the items
	static final String MAX_ID ="max-id";

	private List< Map<String, Property> > instances;
	private DesignPatternType type;
	private int maxId;
	private String name;
	
	public DesignPatternInstanceBean(DesignPatternType dpType, String xmlPath){
		super();
		name = "";
		maxId = 0;
		instances = new ArrayList< Map<String, Property> >();
		type = dpType;
		
		DesignPatternBean dpBean = DesignPattern.getDesignPatternBean(type);
		new DesignPatternInstanceXMLParser(xmlPath, dpBean, this).parse();
	}
	
	public DesignPatternInstanceBean(String instanceName, DesignPatternType dpType){
		super();
		name = instanceName;
		type = dpType;
		maxId = 0;
		instances = new ArrayList< Map<String, Property> >();
	}

	public List< Map<String, Property>> getInstances() {
		return instances;
	}



	public void setInstances(List< Map<String, Property>> instances) {
		this.instances = instances;
	}
	
	public DesignPatternType getType() {
		return type;
	}

	public void setType(DesignPatternType type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public int getMaxId() {
		return maxId;
	}

	public void setMaxId(int maxId) {
		this.maxId = maxId;
	}

	public List<Property> getPropertyByName(String name){
		List<Property> properties = new ArrayList<Property>();
		for (Map<String, Property> instance: instances){
			Property p = instance.get(name);
			if (p!= null){
				properties.add(p);
			}
		}
		return properties;
	}
	
	public void print(){
		for (Map<String,Property> instance: instances){
			System.out.println("instance ============");
			for (Property p: instance.values()){
				System.out.print("property name= " + p.getName() + '\t');
				System.out.print("Label= " + p.getLabel() + '\t');
				System.out.print("Type= " + p.getType() + '\t');
				System.out.print("value= " + p.getValue() + '\n');
			}
		}
	}

	public void deleteInstanceByName(String name, String value) {
		Map <String, Property> deleteInstance = null;
		for (Map<String, Property> instance: instances){
			Property p = instance.get(name);
			if(p!=null && value.equals(p.getValue())){
				deleteInstance = instance;
				break;
			}
		}
		instances.remove(deleteInstance);
		return;
	}

	public Map<String, Property> getInstanceByName(String pName, String pValue) {
		Map <String, Property> editInstance = null;
		for (Map<String, Property> instance: instances){
			Property p = instance.get(pName);
			if( p!=null && pValue.equals(p.getValue())){
					editInstance = instance;
					break;
			}
		}
		return editInstance;
	}
	
	public void save(String xmlPath){
		new DesignPatternInstanceXMLGenerator(xmlPath, this).generate();
	}

	
	
}
